package cn.gson.school.controller.jiaoxue;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.jiaoxue.FangTanDao;
import cn.gson.school.dao.jiaoxue.StudentDao;
import cn.gson.school.dao.xitong.UserInfoDao;
import cn.gson.school.entity.jiaoxue.FangTanEntity;
import cn.gson.school.mappers.jiaoxue.FangTanMappers;

@Controller
@RequestMapping("/fangtan")
public class FangTanController {

	@Autowired
	private FangTanMappers fangTanMappers;

	@Autowired
	private FangTanDao fangtanDao;

	@Autowired
	private StudentDao studentDao;

	@Autowired
	private UserInfoDao userInfoDao;

	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> index(FangTanEntity fangTanEntity,
			@RequestParam(name = "page", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "rows", defaultValue = "20") Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		System.out.println(fangTanEntity);
		List<FangTanEntity> list = fangTanMappers.fangtanList(fangTanEntity);
		PageInfo<FangTanEntity> info = new PageInfo<>(list);
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}

	@RequestMapping("save")
	@ResponseBody
	@Transactional
	public Map<String, Object> save(@Valid FangTanEntity fangTanEntity, HttpServletRequest request, Model model,
			BindingResult br) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败");
			obj.put("errors", br.getFieldErrors());
		} else {
			String classId = request.getParameter("classId");
			String studentName = request.getParameter("studentName");
			String fangTanFangshi = request.getParameter("fangTanFangshi");
			String fangtanContent = request.getParameter("fangtanContent");
			String teacherName = request.getParameter("teacherName");
			String fangTanId = request.getParameter("fangTanId");
			FangTanEntity fangTan;
			if (fangtanContent == null || "".equals(fangtanContent)) {
				obj.put("message", "访谈内容不可以为空哦~不然不能进行操作哟~");
				return obj;
			}
			if (fangTanId == null || "".equals(fangTanId)) {// 添加
				fangTan = new FangTanEntity();
				fangTan.setDelete(1);
				fangTan.setInterviewTime(new Date());
			} else {// 编辑
				fangTan = fangtanDao.findOne(Long.valueOf(fangTanId));
			}
			fangTan.setInterviewMethod(fangTanFangshi);
			fangTan.setInterviewContent(fangtanContent);
			fangTan.setStudentId(studentDao.findByStudentName(studentName));
			fangTan.setUserId(userInfoDao.findByUserName(teacherName));
			fangtanDao.save(fangTan);
			obj.put("success", true);
		}
		return obj;
	}

	@RequestMapping("/delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		FangTanEntity fangTanEntity = fangtanDao.findOne(id);
		if (id != null && fangTanEntity != null) {
			fangTanEntity.setDelete(0);
			fangtanDao.save(fangTanEntity);
			obj.put("success", true);
		} else {
			obj.put("message", "你要删除的记录不存在！");
		}
		return obj;
	}
}
